// 当前等待的渲染列表
import 'dart:async';

import '../builder.dart';
import 'update.dart';
import '../ref.dart';
import 'tools.dart';

List<StreamCollectFactory> _renderWidgetList = [];
Duration _renderDelay = Duration(milliseconds: 1);
dynamic _renderTimer;

_render(List<StreamCollectFactory> tempList) {
  tempList = listUnRepeat(tempList);
  tempList.forEach((ele) {
    try {
      if (!ele.instance.isClosed) {
        ele.instance.sink.add('即将更新');
      }
    } catch (e) {
      print('app wrong: Ref render 调用instance.sink.add(refInstance)出错了！');
    }
  });
}

// 执行页面渲染
renderWidget(List<StreamCollectFactory> renderList, [UpdateOptions? options]) {
  Duration delay = _renderDelay;
  if (options != null && options.delay != null && options.delay is Duration) {
    _renderWidgetList.addAll(renderList);
    delay = options.delay as Duration;
    if (_renderTimer != null && _renderTimer.isActive) _renderTimer.cancel();
    _renderTimer = Timer(delay, () {
      _render(_renderWidgetList);
      _renderWidgetList.clear();
    });
  } else {
    _renderTimer = Timer(delay, () {
      _render(renderList);
    });
  }
}
